package com.hc.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hc.entity.Contract;
import com.hc.entity.House;
import com.hc.entity.Tenant;
import com.hc.service.ContractService;
import com.hc.serviceimp.ContractServiceImpl;


@WebServlet("/ContractServlet")
public class ContractServlet extends HttpServlet {
	//获取service执行对象
	ContractService cService = new ContractServiceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		if (action.equals("toadd")) {
			List<House> houses = cService.getallhouse();
			List<Tenant> tenants = cService.getallteant();
			
			request.setAttribute("house", houses);
			request.setAttribute("tenant", tenants);
			request.getRequestDispatcher("/WEB-INF/contract/add.jsp").forward(request, response);
		}
		if (action.equals("add")) {
			
			String cname = request.getParameter("cname");
			String cdate = request.getParameter("cdate");
			String csdate = request.getParameter("csdate");
			String cedate = request.getParameter("cedate");
			int ctotal = Integer.parseInt(request.getParameter("ctotal"));
			String cway = request.getParameter("cway");
			int cdeposit = Integer.parseInt(request.getParameter("cdeposit"));
			int cnumber = Integer.parseInt(request.getParameter("cnumber"));
			String cman = request.getParameter("cman");
			String cstate = request.getParameter("cstate");
			int hid =Integer.parseInt( request.getParameter("hid"));
			int tid =Integer.parseInt(request.getParameter("tid"));
			
			Contract contract = new Contract(0, cname, cdate, csdate, cedate, ctotal, cway, cdeposit, cnumber, cman, cstate,
					new House(hid, null, null, 0, 0, 0, 0, null, null, null, 0, null, 0, null, null, null, null), 
					new Tenant(tid, null, 0, null, null, null, null,0));
			
			cService.add(contract);
			request.getRequestDispatcher("ContractServlet?action=tolist").forward(request, response);
			
		}
		
		if (action.equals("tolist")) {
			
			request.getRequestDispatcher("/WEB-INF/contract/tolist.jsp").forward(request, response);
		}
		
		if (action.equals("list")) {
			String parameter = request.getParameter("cname");
			String parameter2 = request.getParameter("cdate");
			String parameter3 = request.getParameter("cstate");
			
			List<Contract> contracts = cService.select(parameter, parameter2, parameter3);
			request.setAttribute("list", contracts);
			request.getRequestDispatcher("/WEB-INF/contract/list.jsp").forward(request, response);
		}
		
		if (action.equals("toupdate")) {
			int id =Integer.parseInt(request.getParameter("id"));
			Contract contract = cService.toupdate(id);
			List<House> houses = cService.getallhouse();
			List<Tenant> tenants = cService.getallteant();
			
			request.setAttribute("list", contract);
			request.setAttribute("house", houses);
			request.setAttribute("tenant", tenants);
			request.getRequestDispatcher("/WEB-INF/contract/update.jsp").forward(request, response);
		}
		
		if (action.equals("update")) {
			int cid = Integer.parseInt(request.getParameter("cid"));
			String cname = request.getParameter("cname");
			String cdate = request.getParameter("cdate");
			String csdate = request.getParameter("csdate");
			String cedate = request.getParameter("cedate");
			int ctotal = Integer.parseInt(request.getParameter("ctotal"));
			String cway = request.getParameter("cway");
			int cdeposit = Integer.parseInt(request.getParameter("cdeposit"));
			int cnumber = Integer.parseInt(request.getParameter("cnumber"));
			String cman = request.getParameter("cman");
			String cstate = request.getParameter("cstate");
			int hid =Integer.parseInt( request.getParameter("hid"));
			int tid =Integer.parseInt(request.getParameter("tid"));
			
			Contract contract = new Contract(cid, cname, cdate, csdate, cedate, ctotal, cway, cdeposit, cnumber, cman, cstate,
					new House(hid, null, null, 0, 0, 0, 0, null, null, null, 0, null, 0, null, null, null, null), 
					new Tenant(tid, null, 0, null, null, null, null,0));
			
			cService.update(contract);
			System.out.println("servlet更新完成");
			request.getRequestDispatcher("ContractServlet?action=tolist").forward(request, response);
		}
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
